	package elements.axis ;
	import flash.display.Sprite;
	import elements.axis.YTextField;
	import flash.text.TextFormat;
	//import org.flashdevelop.utils.FlashConnect;
	import string.Utils;

	typedef LABELSTRUCT = {
		var val		: String;
		var pos		: Int;
	};
	
	class YAxisLabelsBase extends Sprite {
		 var steps: Float;
		 var right:Bool;
		 var style:Dynamic;
		
		public function new( values:Array<Dynamic>, steps: Float, json:Dynamic, name:String, axis_name:String ) {
			super();

			this.steps = steps;
			
			var lblStyle:YLabelStyle = new YLabelStyle( json, name );
			this.style = lblStyle.style;
			
			if ( Reflect.hasField(json,axis_name) ) {
				var _axis_name = Reflect.field(json, axis_name);
				// Default to using "rotate" from the y_axis level
				if ( Reflect.hasField(_axis_name, 'rotate') )
					this.style.rotate = _axis_name.rotate;
				// Next override with any values at the y_axis.labels level
				if ( Reflect.hasField(_axis_name, 'labels') )
					ObjectHelper.merge_2( _axis_name.labels, this.style );
			}
			
			// are the Y Labels visible?
			if( !this.style.show_labels )
				return;
			
			// labels
			var pos: Float = 0;
			
			for ( v in values )
			{
				var tmp:YTextField = this.make_label( v );
				tmp.y_val = v.pos;
				this.addChild(tmp);
				
				pos++;
			}
		}

		//
		// use Y Min, Y Max and Y Steps to create an array of
		// Y labels:
		//
		 function make_labels( min: Float, max: Float, right:Bool, steps: Int, lblText:String ):Array<LABELSTRUCT> {
			var values:Array<Dynamic> = []; /// TODO - change this into something that makes sense
			
			var min_: Float = Math.min( min, max );
			var max_: Float = Math.max( min, max );
			
			// hack: hack: http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_13989&sliceId=1
			max_ += 0.000004;
			var tuple : LABELSTRUCT;
			var eek: Int = 0;
			var i : Int;
			
			while (i <= max_)
			{
				tuple = { val:lblText, pos:i };
				values.push( tuple );
				eek++;
				if (eek > 250) break;
				i+=steps;
			}

			return values;
		}
		
		 function make_label( json:Dynamic ):YTextField
		{
			
			
			// does _root already have this textFiled defined?
			// this happens when we do an AJAX reload()
			// these have to be deleted by hand or else flash goes wonky.
			// In an ideal world we would put this code in the object
			// distructor method, but I don't think actionscript has these :-(
			
			var lblStyle:Dynamic = { };
			ObjectHelper.merge_2( this.style, lblStyle );
			ObjectHelper.merge_2( json, lblStyle );
			lblStyle.color = string.Utils.get_color(lblStyle.color);
			
			var tf:YTextField = new YTextField();
			//tf.border = true;
			tf.text = this.replace_magic_values(lblStyle.val, lblStyle.pos);
			var fmt:TextFormat = new TextFormat();
			fmt.color = lblStyle.color;
			fmt.font = lblStyle.rotate == "vertical" ? "spArial" : "Verdana";
			fmt.size = lblStyle.size;
			fmt.align = flash.text.TextFormatAlign.RIGHT;
			tf.setTextFormat(fmt);
			tf.autoSize = flash.text.TextFieldAutoSize.RIGHT;
			if (lblStyle.rotate == "vertical")
			{
				tf.rotation = 270;
				tf.embedFonts = true;
				tf.antiAliasType = flash.text.AntiAliasType.ADVANCED;
			} 
			return tf;
		}

		// move y axis labels to the correct x pos
		public function resize( left: Float, sc:ScreenCoords ):Void
		{
		}


		public function get_width(): Float{
			var max: Float = 0;
			for ( i in 0...this.numChildren )
			{
				var tf:YTextField = cast(this.getChildAt(i),YTextField);
				max = Math.max( max, tf.width );
			}
			return max;
		}
		
		public function die(): Void {
			
			while ( this.numChildren > 0 )
				this.removeChildAt(0);
		}

		 function replace_magic_values(labelText:String, yVal: Int):String {
			labelText = StringTools.replace(labelText,'#val#', NumberUtils.formatNumber(yVal));
			return labelText;
		}
	}
